﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Word = Microsoft.Office.Interop.Word;

namespace lcs.Web.DemandPlan
{
    public partial class PrintDemandPlan : System.Web.UI.Page
    {
        lcs.BLL.demandplaninfo bll = new lcs.BLL.demandplaninfo();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
                {
                    string strid = Request.Params["id"];
                    int dpid = (Convert.ToInt32(strid));

                    DataSet ds = bll.GetList(" and dpid=" + dpid);

                    if (ds.Tables[0].Rows.Count == 1)
                    {
                        this.txt_orderNum.Text = ds.Tables[0].Rows[0]["ordernum"].ToString();
                    }
                  
                }
            }
        }

        private void ExportWord(int dpid)
        {
            
            DataSet ds = bll.GetList(" and dpid=" + dpid);

            if (ds.Tables[0].Rows.Count == 1)
            {
                string cgddh = ds.Tables[0].Rows[0]["ordernum"].ToString();//采购订单号

                Word._Application app = new Word.Application();

                string templatePath = System.Web.HttpContext.Current.Server.MapPath("~/template/");//模板路径
                string savePath = System.Web.HttpContext.Current.Server.MapPath("~/exportWords/");

                string tempName = templatePath + "contractElc.doc";

                string newName = savePath  + cgddh  + ".doc";//生成一个新的文件的路径名

                Maticsoft.BLL.ExportWordOp wordOp = new Maticsoft.BLL.ExportWordOp();

                wordOp.CreateNewDocument(tempName);

                lcs.BLL.demandmaterialinfo mbll = new lcs.BLL.demandmaterialinfo();
                DataSet mds = mbll.GetList(" and dpid=" + dpid);

                //项目名称
                string xmmc = ds.Tables[0].Rows[0]["projectName"].ToString();
                wordOp.InsertValue("xmmc", xmmc);

                //招标编号
                string xmbh = ds.Tables[0].Rows[0]["pbh"].ToString();
                wordOp.InsertValue("xmbh", xmbh);

                //采购订单号
                //string cgddh = ds.Tables[0].Rows[0]["ordernum"].ToString();
                wordOp.InsertValue("cgddh", cgddh);

                if (mds.Tables[0].Rows.Count > 0)
                {
                    DataTable mdt = mds.Tables[0];

                    string bookmark = "materailinfo";//物料信息表格

                    Word.Table tblw = wordOp.InsertTable(bookmark, 1, 8, 540.0f, true);//插入一个外围表格

                    CreatWordTable(wordOp, tblw, mdt);
                   
                }


                wordOp.SaveDocument(newName);
            }
           
           
        
        }


        //创建word表格
        private void CreatWordTable(Maticsoft.BLL.ExportWordOp wordOp, Word.Table tbl, DataTable dt)
        {
            //行首，第一行
            wordOp.InsertCell(tbl, 1, 1, "序号");
            wordOp.InsertCell(tbl, 1, 2, "物料描述");
            wordOp.InsertCell(tbl, 1, 3, "单位");
            wordOp.InsertCell(tbl, 1, 4, "数量");
            wordOp.InsertCell(tbl, 1, 5, "单价");
            wordOp.InsertCell(tbl, 1, 6, "总价");
            wordOp.InsertCell(tbl, 1, 7, "交货期");
            wordOp.InsertCell(tbl, 1, 8, "备注");

            //内容行
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                wordOp.AddRow(tbl);

                DataRow r = dt.Rows[i];
                wordOp.InsertCell(tbl, i + 2, 1, (i + 1).ToString());//序号
                wordOp.InsertCell(tbl, i + 2, 2, r["mms"].ToString());//物料描述
                wordOp.InsertCell(tbl, i + 2, 3, r["munit"].ToString());//单位
                wordOp.InsertCell(tbl, i + 2, 4, r["num"].ToString());//数量
                wordOp.InsertCell(tbl, i + 2, 5, r["price"].ToString());//单价

                decimal totalPrice = Convert.ToInt32(r["num"]) * Convert.ToDecimal(r["price"]);
                wordOp.InsertCell(tbl, i + 2, 6, totalPrice.ToString());//总价

                wordOp.InsertCell(tbl, i + 2, 7, Convert.ToDateTime(r["deliverytime"]).ToShortDateString());//交货期
               
                //设置内容行样式
                wordOp.SetCellStyle(tbl, i + 2, 1, Word.WdColorIndex.wdAuto, Word.WdColorIndex.wdBlack, 0, 10.0f, 40.0f, "center");
                wordOp.SetCellStyle(tbl, i + 2, 2, Word.WdColorIndex.wdAuto, Word.WdColorIndex.wdBlack, 0, 10.0f, 150.0f, "center");
                wordOp.SetCellStyle(tbl, i + 2, 3, Word.WdColorIndex.wdAuto, Word.WdColorIndex.wdBlack, 0, 10.0f, 40.0f, "center");
                wordOp.SetCellStyle(tbl, i + 2, 4, Word.WdColorIndex.wdAuto, Word.WdColorIndex.wdBlack, 0, 10.0f, 60.0f, "center");
                wordOp.SetCellStyle(tbl, i + 2, 5, Word.WdColorIndex.wdAuto, Word.WdColorIndex.wdBlack, 0, 10.0f, 60.0f, "center");
                wordOp.SetCellStyle(tbl, i + 2, 6, Word.WdColorIndex.wdAuto, Word.WdColorIndex.wdBlack, 0, 10.0f, 60.0f, "center");
                wordOp.SetCellStyle(tbl, i + 2, 7, Word.WdColorIndex.wdAuto, Word.WdColorIndex.wdBlack, 0, 10.0f, 80.0f, "center");
                wordOp.SetCellStyle(tbl, i + 2, 8, Word.WdColorIndex.wdAuto, Word.WdColorIndex.wdBlack, 0, 10.0f, 50.0f, "center");

                //设置首行的样式
                wordOp.SetCellStyle(tbl, 1, 1, Word.WdColorIndex.wdAuto, Word.WdColorIndex.wdBlack, 0, 12.0f, 40.0f, "center");
                wordOp.SetCellStyle(tbl, 1, 2, Word.WdColorIndex.wdAuto, Word.WdColorIndex.wdBlack, 0, 12.0f, 150.0f, "center");
                wordOp.SetCellStyle(tbl, 1, 3, Word.WdColorIndex.wdAuto, Word.WdColorIndex.wdBlack, 0, 12.0f, 40.0f, "center");
                wordOp.SetCellStyle(tbl, 1, 4, Word.WdColorIndex.wdAuto, Word.WdColorIndex.wdBlack, 0, 12.0f, 60.0f, "center");
                wordOp.SetCellStyle(tbl, 1, 5, Word.WdColorIndex.wdAuto, Word.WdColorIndex.wdBlack, 0, 12.0f, 60.0f, "center");
                wordOp.SetCellStyle(tbl, 1, 6, Word.WdColorIndex.wdAuto, Word.WdColorIndex.wdBlack, 0, 12.0f, 60.0f, "center");
                wordOp.SetCellStyle(tbl, 1, 7, Word.WdColorIndex.wdAuto, Word.WdColorIndex.wdBlack, 0, 12.0f, 80.0f, "center");
                wordOp.SetCellStyle(tbl, 1, 8, Word.WdColorIndex.wdAuto, Word.WdColorIndex.wdBlack, 0, 12.0f, 50.0f, "center");

                
            }

            //合并“备注”单元格
            if (dt.Rows.Count > 1)
            {
                wordOp.MergeCell(tbl, 2, 8, dt.Rows.Count + 1, 8);
            }
           

            //增加合计行

            decimal hj = 0;

            foreach (DataRow row in dt.Rows)
            {
                hj += (Convert.ToInt32(row["num"]) * Convert.ToDecimal(row["price"]));
            }

            double hj_double = Convert.ToDouble(hj);

            hj_double = Math.Round(hj_double, 2);//四舍五入取两位

            string hjstr = lcs.BLL.ZhHConvert.NumGetStr(hj_double);

            string str = "合计（大写）：" + hjstr + "                                ￥" + hj_double;

            wordOp.AddRow(tbl);
            wordOp.InsertCell(tbl, dt.Rows.Count + 2, 1, str);
            wordOp.MergeCell(tbl, dt.Rows.Count + 2, 1, dt.Rows.Count + 2, 8);
            wordOp.SetCellStyle(tbl, dt.Rows.Count + 2, 1, Word.WdColorIndex.wdAuto, Word.WdColorIndex.wdBlack, 0, 10.0f, 540.0f, "left");
           
            
        }

        //生成合同
        protected void lbn_contract_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txt_orderNum.Text))
            {
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "alert", "alert('请填写采购订单号！')", true);
                
            }
            else
            {
                if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
                {
                    string strid = Request.Params["id"];
                    int dpid = (Convert.ToInt32(strid));

                    lcs.Model.demandplaninfo d = bll.GetModel(dpid);

                    d.ordernum = this.txt_orderNum.Text;

                    if (bll.UpdateModelBidSubmit(d))//更新采购订单号
                    {
                        ExportWord(dpid);

                        //this.lbn_contract.Visible = false;
                        this.pnl_contract.Visible = false;
                        this.hy_download.Visible = true;
                        this.hy_download.NavigateUrl = "~/exportWords/"+d.ordernum +".doc";
                    }

                }
               
            }
        }
    }
}